<template>
  <div>
    <CrumBsofbread name1="商品管理" name2="商品列表"/>
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input 
            placeholder="请输入内容"
            v-model="querInfo.query"
            clearable
            @clear="getGoodsList"
          >
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
          </el-input> 
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="goAddpage"
            >添加商品</el-button
          >
        </el-col>
      </el-row>
      <!-- 表格 -->
      <el-table :data="goodslist" border stripe>
        <el-table-column label="序号" type="index"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name"></el-table-column>
        <el-table-column label="价格(元)" prop="goods_price" width="110px"></el-table-column>
        <el-table-column label="商品重量" prop="goods_weight" width="70px"></el-table-column>
        <el-table-column label="创建时间" prop="add_time" width="140px">
          <template slot-scope="scope">
            {{scope.row.add_time | dateFormat}}
          </template>
        </el-table-column>
        <el-table-column label="操作" prop="level" width="140px">
          <template slot-scope="scope">
            <!-- 修改按钮 -->
            <el-button type="primary" size="mini" 
            icon="el-icon-edit" @click="showEdit(scope.row)"></el-button>
            <!-- 删除按钮 -->
            <el-button type="danger" size="mini" 
            icon="el-icon-delete" @click="removeUser(scope.row.goods_id)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="querInfo.pagenum"
        :page-sizes="[5, 8, 10, 15, 20]"
        :page-size="querInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import CrumBsofbread from '../CrumBsofbread.vue'
export default {
  components: {
    CrumBsofbread
  },
  data() {
    return {
      querInfo: {
        query: '',
        pagenum: 1,
        pagesize: 8
      },
      //商品列表数据
      goodslist: [],
      //总共商品条数
      total: 0
    }
  },
  created() {
    this.getGoodsList()
  },
  methods: {
    async getGoodsList() {
      const { data: res } = await this.$http.get('goods', {params: this.querInfo})
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品列表失败！')
      }
      this.$message.success('获取商品列表成功！')
      //console.log(res.data);
      this.goodslist = res.data.goods
      this.total = res.data.total
    },
     //监听每页显示条数事件
    handleSizeChange(newSize) {
      this.querInfo.pagesize = newSize
      this.getGoodsList()
    },
    //监听页码事件
    handleCurrentChange(newPage) {
      this.querInfo.pagenum = newPage
      this.getGoodsList()
    },
    //删除按钮
    async removeUser(id) {
      //弹出确定取消框，是否删除用户
      const confirmResult = await this.$confirm(
        "请问是否要永久删除该用户",
        "删除提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      if (confirmResult != 'confirm') {
        return this.$message.info("已经取消删除")
      }
      //发送请求根据id完成删除操作
      const { data: res } = await this.$http.delete(`goods/${id}`);
      if (res.meta.status !== 200) return this.$message.error("删除商品失败! ")
      this.$message.success("删除商品成功")
      this.getGoodsList()
    },
    goAddpage() {
      this.$router.push('/goods/AddGoods')
    }
  }
}
</script>

<style>

</style>